const db = getApp().globalData.db
import Toast from '@vant/weapp/toast/toast';
// pages/issue/issue.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bookName: '',
    author: '',
    category: '',
    introduction: '',
    dir: '',
    pickerShow: false,
    columns: [],
    fileList: [],
    fileID: ''
  },

  //点击返回
  Back() {
    wx.navigateBack()
  },

  //弹出框
  openPicker() {
    this.setData({ pickerShow: true })
  },

  //取消按钮
  onCanccel() {
    this.setData({ pickerShow: false })
  },

  pickerClose() {
    this.onCanccel()
  },
  //确定按钮
  onConfirm(ev) {
    this.setData({
      category: this.data.columns[ev.detail.index],
      pickerShow: false
    })
  },

  //读取文件信息
  afterRead(ev) {
    //获取上传的文件对象
    const file = ev.detail.file

    //预览图片 显示的路径
    this.data.fileList.push({
      url: ev.detail.file.url,
      name: ev.detail.name,
      status: 'uploading',//上传状态
      message: '上传中'
    })
    //重新赋值
    this.setData({ fileList: this.data.fileList })

    //获取文件名
    const fileName = file.url.match(/\/([^\/]+)$/)[1]


    //获取文件对象
    const fileInfo = this.data.fileList[0]

    wx.cloud.uploadFile({
      cloudPath: 'cover/' + fileName, // 云存储的路径
      filePath: file.url, //上传的文件路径
      success: res => {
        //成功对话框

        //文件ID
        const fileID = res.fileID

        //修改文件的状态
        fileInfo.status = 'done'
        fileInfo.message = '上传成功'
        this.setData({
          fileID
        })
      },
      fail: res => {
        //修改状态
        fileInfo.status = 'fail'
        fileInfo.message = '上传失败'
        console.log(res);
      },
      complete: () => {
        this.setData({ fileList: this.data.fileList })
      }
    })

  },

  //删除图片
  deleteFile(ev) {
    const index = ev.detail.index
    this.data.fileList.splice(index, 2)
    this.setData({ fileList: this.data.fileList })
  },

  //提交按钮
  submit() {
    Toast.loading({
      message: '提交中...',
      forbidClick: true,
    });
    const books = db.collection("books")
    books.add({
      data: {
        bookName: this.data.bookName,
        author: this.data.author,
        category: this.data.category,
        introduction: this.data.introduction,
        dir: this.data.dir,
        fileID: this.data.fileID,
        updateTime: new Date().getTime(),
        createTime: new Date().getTime()
      },
      success: res => {
        //成功
        // this.reset()
        Toast.success('提交成功');
      },
      fail: err => {
        //失败
      },
      complete: () => {
        setTimeout(() => {
          //关闭提示
          Toast.clear(true)
        }, 2000)
      }
    })
  },

  //重置
  reset() {
    this.setData({
      bookName: '',
      author: '',
      category: '',
      introduction: '',
      dir: '',
      pickerShow: false,
      columns: [],
      fileList: [],
      fileID: ''
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    db.collection("category").get({
      success: res => {
        let data = []
        res.data.forEach(element => {
          data.push(element.category)
        });
        this.setData({ columns: data })
      },
      fail: err => {
        console.log(err);
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})